package com.lanou.renrenxin.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMContactManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou.renrenxin.R;
import com.lanou.renrenxin.adapter.AddFriendAdapter;
import com.lanou.renrenxin.base.BaseActivity;
import com.lanou.renrenxin.custommodule.RotateLoading;
import com.lanou.renrenxin.mybomb.MyUser;
import com.lanou.renrenxin.onclicklistener.OnAddFriendItemClickListener;
import com.lanou.renrenxin.singleton.RenrenxinSingleton;
import com.lanou.renrenxin.tools.DividerItemDecoration;
import com.lanou.renrenxin.tools.LogUtils;
import com.parse.ParseAnalytics;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zy on 16/3/4.
 * 搜索好友,显示好友请求activity
 */
public class AddContactActivity extends BaseActivity implements View.OnClickListener, TextWatcher, OnAddFriendItemClickListener {
    private EditText editTextsearch;
    private ImageButton btnSearch;
    private RecyclerView recyclerView;
    private RotateLoading rotateLoading;
    private AddFriendAdapter adapter;
    private Handler handler;
    private List<MyUser> userList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ParseAnalytics.trackAppOpenedInBackground(getIntent());
        setContentView(R.layout.activity_addcontact);


        editTextsearch = (EditText) findViewById(R.id.et_activity_addcontact_username);
        recyclerView = (RecyclerView) findViewById(R.id.recycle_activity_addcontact);
        rotateLoading = (RotateLoading) findViewById(R.id.rotateloading_activity_addcontact);

        findViewById(R.id.imgBtn_activity_addcontact_back).setOnClickListener(this);
        findViewById(R.id.imgBtn_activity_addcontact_search).setOnClickListener(this);
        editTextsearch.addTextChangedListener(this);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        adapter = new AddFriendAdapter(this);
        adapter.setOnAddFriendItemClickListener(this);
        recyclerView.setAdapter(adapter);

        userList = new ArrayList<>();
        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                if (msg.what == 756) {
                    userList = (List<MyUser>) msg.obj;
                    adapter.refreshList(userList);
                }
                return false;
            }
        });





    }


    @Override
    protected void onResume() {
        super.onResume();

    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imgBtn_activity_addcontact_back:
                finish();
                break;
        }
    }


    //添加联系人
    private void addContact(final String username, final String reason) {
//        //参数为要添加的好友的username和添加理由
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    EMContactManager.getInstance().addContact(username, reason);
                    LogUtils.e("ADD", "添加朋友");
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }


            }
        }).start();

    }


    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if (!editTextsearch.getText().equals("") && editTextsearch.getText() != null) {
            RenrenxinSingleton.getInstence().queryMatchsUsername(editTextsearch.getText().toString(), handler);
        }

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onItemClick(int pos) {
        showAddFriendDialog(pos);
    }

    private void showAddFriendDialog(final int pos) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_add_friend, null);
        ImageView headImg = (ImageView) view.findViewById(R.id.iv_dialog_add_friend_headimg);
        TextView usernameTv = (TextView) view.findViewById(R.id.tv_dialog_add_friend_username);
        final EditText reasonEt = (EditText) view.findViewById(R.id.et_dialog_add_friend_reason);

        usernameTv.setText(userList.get(pos).getUsername());

        builder.setView(view);
        builder.setTitle("添加理由");
        builder.setPositiveButton("添加", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                addContact(userList.get(pos).getUsername(), reasonEt.getText().toString());
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show();
    }

}





